package com.huabo.monitor.controller;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.hbfk.entity.DealUserToken;
import com.hbfk.entity.TblStaffUtil;
import com.hbfk.util.JsonBean;
import com.hbfk.util.ResponseFormat;
import com.huabo.monitor.oracle.entity.TblAssesselement;
import com.huabo.monitor.oracle.entity.TblOrganization;
import com.huabo.monitor.service.TblAssEleCategoryService;
import com.huabo.monitor.service.TblAssessElementService;
import com.huabo.monitor.util.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(value = "内部控制", tags = {"内部控制相关"})
@RequestMapping(value = "/nbkz")
public class FactorsController {
    @Resource
    private TblAssessElementService tblAssessElementService;

    @Resource
    private TblAssEleCategoryService tblAssEleCategoryService;


    /**
     * 要素维护 列表
     *
     * @param request
     * @return
     */
    @GetMapping(value = "/gzdg/def_basic_list")
    @ApiOperation("要素维护列表-内容页面分页功能")
    public JsonBean getgzdgBasicList(HttpServletRequest request,
                                     @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token,
                                     @ApiParam(name = "pageNumber", value = "分页当前页数", required = false) @RequestParam(value = "pageNumber", required = false, defaultValue = "1") Integer pageNumber,
                                     @ApiParam(name = "pageSize", value = "每页记录数", required = false) @RequestParam(value = "pageSize", required = false, defaultValue = "15") Integer pageSize,
                                     @ApiParam(name = "businesstype", value = "业务类别", required = false) @RequestParam(value = "businesstype", required = false) String businesstype,
                                     @ApiParam(name = "elementNumber", value = "要素编号", required = false) @RequestParam(value = "elementNumber", required = false) String elementNumber,
                                     @ApiParam(name = "elementname", value = "要素名称", required = false) @RequestParam(value = "elementname", required = false) String elementname,
                                     @ApiParam(name = "auditpoint", value = "审查要点", required = false) @RequestParam(value = "auditpoint", required = false) String auditpoint) {
        JsonBean jsonBean = null;
        try {
            TblAssesselement assesselement = new TblAssesselement();
            assesselement.setBusinesstype(businesstype);
            assesselement.setElementname(elementname);
            assesselement.setElementNumber(elementNumber);
            assesselement.setAuditpoint(auditpoint);
            jsonBean = tblAssessElementService.findByPageInfo(pageNumber, pageSize, assesselement);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseFormat.retParam(1, 1000, e.getMessage());
        }
        return jsonBean;
    }


    /**
     * 要素删除
     *
     * @return
     */
    @DeleteMapping(value = "/gzdg/def_basic_deleteIds", produces = "application/json; charset=utf-8")
    @ApiOperation("要素删除")
    public JsonBean def_basic_del(@ApiParam(name = "deleteIds", value = "要素id", required = true) @RequestParam(value = "deleteIds", required = true) String deleteIds) {
        try {
            return tblAssessElementService.deleteByIds(deleteIds);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseFormat.retParam(0, 1000, e.getMessage());
        }
    }

    /**
     * 要素新增保存
     *
     * @param assesselement
     * @return
     */
    @RequestMapping(value = "/gzdg/def_basic_save", produces = "application/json; charset=utf-8", method = {RequestMethod.POST})
    @ApiOperation("要素新增 || 跟新-保存")
    public JsonBean gzdg_def_basic_add(@ApiParam(name="assesselement",value="实体",required=true) TblAssesselement assesselement,
                                       @ApiParam(name="token",value="登录用户token",required=false) @RequestHeader("token")String token){
        if (StringUtils.isNotBlank(assesselement.getElementNumber())) {
            TblStaffUtil loginStaff = null;
            try {
                loginStaff = DealUserToken.parseUserToken(token);
                if(loginStaff == null) {
                    return ResponseFormat.retParam(0,20006,null);
                }
            } catch (Exception e) {
                e.printStackTrace();
                ResponseFormat.retParam(1,1000,e.getMessage());
            }

            Integer orgid = loginStaff.getCurrentOrg().getOrgid().intValue();

            //根据条件获取 TblAssesselement
            TblAssesselement tblAssesselement = this.tblAssessElementService.getNumber(assesselement.getElementNumber(),
                    orgid.toString());
            //如果存在则跟新 如果不存在则保存
            if (null == tblAssesselement) {
                assesselement.setTblComany(orgid.toString());
                this.tblAssessElementService.add(assesselement);
                //return JsonBean.success();
                return ResponseFormat.retParam(1, 200, "添加操作成功");
            } else if (tblAssesselement.getAsseleid().equals(assesselement.getAsseleid())) {
                tblAssesselement.setTblComany(tblAssesselement.getTblComany());
                tblAssesselement.setAssessrules(assesselement.getAssessrules());
                tblAssesselement.setAuditpoint(assesselement.getAuditpoint());
                tblAssesselement.setBusinesstype(assesselement.getBusinesstype());
                tblAssesselement.setBusinessattribute(assesselement.getBusinessattribute());
                tblAssesselement.setElementname(assesselement.getElementname());
                tblAssesselement.setStatus(assesselement.getStatus());
                this.tblAssessElementService.update(tblAssesselement);
                return ResponseFormat.retParam(1, 200, " 更新操作成功");
            } else {
                //return JsonBean.error("该编号已经存在");
                return ResponseFormat.retParam(0, 50003, "该编号已经存在");
            }
        }
        //return JsonBean.error("要素编号不能为空");
        return ResponseFormat.retParam(0, 10004, "要素编号不能为空");
    }

    /**
     * 要素维护导出 有选择项则导出选择的，没有则导出全部
     *
     * @param request
     * @param response
     */
    @RequestMapping(value = "/gzdg/yswhexport", produces = "application/json; charset=utf-8")
    @ApiOperation("要素维护导出")
    public void exportYswh(HttpServletRequest request, HttpServletResponse response,
                           @ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token")String token) {
        TblStaffUtil loginStaff = null;
        try {
            loginStaff = DealUserToken.parseUserToken(token);
            if(loginStaff == null) {
                return;
            }
        } catch (Exception e) {
            e.printStackTrace();
            ResponseFormat.retParam(1,1000,e.getMessage());
        }

        String eleid = request.getParameter("eleid");
        TblOrganization attribute = (TblOrganization) request.getSession().getAttribute("hbOrgEntity");// 选则的机构
        List<TblAssesselement> list = null;
        if (eleid != null && StringUtils.isNotBlank(eleid)) {
            list = tblAssessElementService.getAssEssByIn(eleid);
        } else {
            list = tblAssessElementService.getComany(attribute.getOrgid().toString());
        }
        String[] cNames = {"要素编号", "要素名称", "业务类别", "评分规则", "审查要点"};
        ExcelUtil exportUtil = new ExcelUtil("控制矩阵", cNames);
        HSSFCell cell = null;
        HSSFCellStyle bodyStyle = exportUtil.getBodyStyle();

        for (int j = 0; j < list.size(); j++) {
            HSSFRow bodyRow = exportUtil.getNextRow(j + 1);
            TblAssesselement c = (TblAssesselement) list.get(j);
            // 要素编号
            cell = bodyRow.createCell(0);
            cell.setCellStyle(bodyStyle);
            cell.setCellValue(c.getElementNumber());
            // 要素名称
            cell = bodyRow.createCell(1);
            cell.setCellStyle(bodyStyle);
            cell.setCellValue(c.getElementname());
            // 业务类别
            cell = bodyRow.createCell(2);
            cell.setCellStyle(bodyStyle);
            cell.setCellValue(c.getBusinesstype());
            // 评分规则
            cell = bodyRow.createCell(3);
            cell.setCellStyle(bodyStyle);
            cell.setCellValue(c.getAssessrules());
            // 审查要点
            cell = bodyRow.createCell(4);
            cell.setCellStyle(bodyStyle);
            cell.setCellValue(c.getAuditpoint());
        }
        exportUtil.writeOut(response, "要素维护.xls");
    }

}
